﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.util.*;

    public class RepaintManager {

        private static var instance:RepaintManager;

        private var repaintQueue:HashSet;
        private var validateQueue:HashSet;
        private var timer:Timer;
        private var renderring:Boolean;
        private var alwaysUseTimer:Boolean;

        public function RepaintManager(){
            if (instance != null){
                throw (new Error("Singleton can't be create more than once!"));
            };
            this.repaintQueue = new HashSet();
            this.validateQueue = new HashSet();
            this.renderring = false;
            this.alwaysUseTimer = false;
            this.timer = new Timer(19, 1);
            this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.__timerRender);
        }
        public static function getInstance():RepaintManager{
            if (instance == null){
                instance = new (RepaintManager)();
            };
            return (instance);
        }

        public function setAlwaysUseTimer(_arg1:Boolean, _arg2:int=19):void{
            this.alwaysUseTimer = _arg1;
            this.timer.delay = _arg2;
        }
        public function addRepaintComponent(_arg1:Component):void{
            this.repaintQueue.add(_arg1);
            this.renderLater(_arg1);
        }
        public function addInvalidComponent(_arg1:Component):void{
            var _local2:Component = this.getValidateRootComponent(_arg1);
            if (_local2 != null){
                this.validateQueue.add(_local2);
                this.renderLater(_arg1);
            };
        }
        public function addInvalidRootComponent(_arg1:Component):void{
            this.validateQueue.add(_arg1);
            this.renderLater(_arg1);
        }
        private function renderLater(_arg1:Component):void{
            var _local2:Stage = _arg1.stage;
            if (((((this.alwaysUseTimer) || ((_local2 == null)))) || (this.renderring))){
                if (!this.timer.running){
                    this.timer.reset();
                    this.timer.start();
                };
            } else {
                _local2.addEventListener(Event.RENDER, this.__render, false, 0, true);
                _local2.invalidate();
            };
        }
        private function getValidateRootComponent(_arg1:Component):Component{
            var _local3:Component;
            var _local2:Component;
            _local3 = _arg1;
            while (_local3 != null) {
                if (_local3.isValidateRoot()){
                    _local2 = _local3;
                    break;
                };
                _local3 = _local3.getParent();
            };
            _local3 = _local2;
            while (_local3 != null) {
                if (!_local3.isVisible()){
                };
                _local3 = _local3.getParent();
            };
            return (_local2);
        }
        private function __timerRender(_arg1:TimerEvent):void{
            this.__render();
            _arg1.updateAfterEvent();
        }
        private function __render(_arg1:Event=null):void{
            var _local2:int;
            var _local3:int;
            var _local4:Component;
            var _local7:Stage;
            if (_arg1){
                _local7 = (_arg1.currentTarget as Stage);
                _local7.removeEventListener(Event.RENDER, this.__render);
            };
            this.renderring = true;
            var _local5:Array = this.validateQueue.toArray();
            this.validateQueue.clear();
            _local3 = _local5.length;
            _local2 = -1;
            if (_local3 > 0){
            };
            while (++_local2 < _local3) {
                _local4 = _local5[_local2];
                _local4.validate();
            };
            var _local6:Array = this.repaintQueue.toArray();
            this.repaintQueue.clear();
            _local3 = _local6.length;
            _local2 = -1;
            while (++_local2 < _local3) {
                _local4 = _local6[_local2];
                _local4.paintImmediately();
            };
            this.renderring = false;
        }

    }
}//package org.aswing 
